package com.robooot.business_counter.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Component
public class I18nUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (I18nUtil.applicationContext == null) {
            I18nUtil.applicationContext = applicationContext;
        }
    }

    public static String getMessage(String path, Object... args) {
        MessageSource messageSource = applicationContext.getBean(MessageSource.class);
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(path, args, locale);
    }

    public static String getMessage(String path,Locale locale,Object... args) {
        MessageSource messageSource = applicationContext.getBean(MessageSource.class);
        return messageSource.getMessage(path, args, locale);
    }
}
